package com.example.controller;

import com.example.common.Result;
import com.example.entity.Order;
import com.example.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    private final OrderService orderService;
    
    @Value("${server.port}")
    private String serverPort;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping("/create")
    public Result<Order> create(@RequestBody Order order) {
        Order result = orderService.create(order);
        log.info("创建订单：" + result);
        return Result.success(result);
    }

    @GetMapping("/get/{id}")
    public Result<Order> getOrderById(@PathVariable("id") Long id) {
        Order order = orderService.getOrderById(id);
        log.info("查询订单：" + order);
        if (order != null) {
            return Result.success(order);
        } else {
            return Result.error("没有对应记录");
        }
    }

    @PostMapping("/pay/{id}")
    public Result<Order> pay(@PathVariable("id") Long id) {
        Order order = orderService.pay(id);
        if (order != null) {
            return Result.success(order);
        } else {
            return Result.error("支付失败");
        }
    }

    @GetMapping("/port")
    public String getPort() {
        return serverPort;
    }
} 